import 'package:bilibili_getx_project/ui/pages/functions/mini_window_video_player/mini_window_video_player_logic.dart';
import 'package:bilibili_getx_project/ui/pages/functions/mini_window_video_player/mini_window_video_player_view.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_floating/floating/floating.dart';
import 'package:get/get.dart';

import 'video_player_example_state.dart';

class VideoPlayerExampleLogic extends GetxController {
  final VideoPlayerExampleState state = VideoPlayerExampleState();

  @override
  void onReady() {
    state.videoPlayerController.initialize().then((value) {
      state.videoPlayerController.play();
      state.isPlaying = true;
      update();
    });
    state.videoPlayerController.addListener(() {
      state.isPlaying = state.videoPlayerController.value.isPlaying;
    });
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }

  createMiniWindowVideo(BuildContext context) {
    state.floating = state.floatingManager.createFloating('1', Floating(
      MiniWindowVideoPlayerView(),
      isShowLog: false,
    ));
    state.floating.open(context);
    state.openMiniWindow = true;
    update();

    ///传递参数给小窗口
    MiniWindowVideoPlayerLogic miniWindowVideoPlayerLogic = Get.find<MiniWindowVideoPlayerLogic>();
    miniWindowVideoPlayerLogic.state.videoPlayerController = state.videoPlayerController;
    miniWindowVideoPlayerLogic.state.floating = state.floating;
    miniWindowVideoPlayerLogic.state.isPlaying = state.isPlaying;
    miniWindowVideoPlayerLogic.state.videoPlayerController.addListener(() {
      if(miniWindowVideoPlayerLogic.state.videoPlayerController.value.isPlaying) {
        miniWindowVideoPlayerLogic.update();
      }
    });
    ///隐藏按钮（暂停时显示按钮、正在播放时就隐藏按钮）
    if(state.isPlaying) {
      miniWindowVideoPlayerLogic.startShowButtons();
    }
  }
}
